package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallPayOrderRecord;
import com.sc.nft.enums.PayStatusEnum;
import com.sc.nft.enums.PayTypeEnum;
import com.sc.nft.mapper.MallPayOrderRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * IP商城支付回调表(sc_mall_pay_order_record)数据DAO
 *
 * @author Sun
 * @since 2023-05-15 16:28:44
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class MallPayOrderRecordDao extends ServiceImpl<MallPayOrderRecordMapper, MallPayOrderRecord> {

    public MallPayOrderRecord getPaySuccessByOrderNo(String orderNo) {
        LambdaQueryWrapper<MallPayOrderRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPayOrderRecord::getOrderNo, orderNo);
        wrapper.eq(MallPayOrderRecord::getStatus, PayStatusEnum.SUCCESS);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public MallPayOrderRecord getNewPaySuccessByOrderNo(String orderNo) {
        LambdaQueryWrapper<MallPayOrderRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPayOrderRecord::getOrderNo, orderNo);
        wrapper.eq(MallPayOrderRecord::getOrderType, PayTypeEnum.PAY);
        wrapper.eq(MallPayOrderRecord::getStatus, PayStatusEnum.SUCCESS);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public MallPayOrderRecord getPaySuccessById(Long id) {
        LambdaQueryWrapper<MallPayOrderRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPayOrderRecord::getId, id);
        wrapper.eq(MallPayOrderRecord::getOrderType, PayTypeEnum.PAY);
        wrapper.eq(MallPayOrderRecord::getStatus, PayStatusEnum.SUCCESS);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public MallPayOrderRecord getPayByOrderNo(String orderNo) {
        LambdaQueryWrapper<MallPayOrderRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPayOrderRecord::getOrderNo, orderNo);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public MallPayOrderRecord getPayByOutTradeNo(String outTradeNo) {
        LambdaQueryWrapper<MallPayOrderRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPayOrderRecord::getOutTradeNo, outTradeNo);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }

    public MallPayOrderRecord getByTradeNo(String tradeNo) {
        LambdaQueryWrapper<MallPayOrderRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallPayOrderRecord::getTradeNo, tradeNo);
        return getOne(wrapper);
    }

    public BigDecimal getPayAmount(Long userId){
        return baseMapper.getPayAmount(userId);
    }
}